<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="就餐类型" prop="type" >
        <el-select v-model="queryParams.type" placeholder="请选择就餐类型" clearable size="small" @change="handleQuery">
          <!-- <el-option label="全部" value="" /> -->
          <el-option label="早餐" value="1" />
          <el-option label="午餐" value="2" />
          <el-option label="晚餐" value="3" />
          <el-option label="接待餐" value="4" />
<!--          <el-option label="其它" value="5" />-->
        </el-select>
      </el-form-item>

       <!-- @keyup.enter.native="handleQuery" -->

      <el-form-item label="开始时间" prop="startTime" >
        <el-time-select style="width: 100%;" @change="handleQuery"
          placeholder="开始时间"
          v-model="queryParams.startTime"
          :picker-options="{
            start: '06:30',
            step: '00:15',
            end: '23:30'
          }">
      </el-time-select>
      </el-form-item>

      <el-form-item label="结束时间" prop="endTime">
        <el-time-select style="width: 100%;" @change="handleQuery"
          placeholder="结束时间"
          v-model="queryParams.endTime"
          :picker-options="{
            start: '06:30',
            step: '00:15',
            end: '23:30',
            minTime: queryParams.startTime
          }">
      </el-time-select>
    </el-form-item>



      <el-form-item label="所属公司" prop="companyId">
        <el-select v-model="queryParams.companyId" placeholder="请选择所属公司" clearable size="small" @change="handleQuery">
          <el-option v-for="item in companys" :key="item.id" :label="item.text" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:diningtype:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:diningtype:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:diningtype:delete']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:diningtype:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed label="序号" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template>
      </el-table-column>
      <el-table-column label="就餐类型" align="center" prop="type">
            <template slot-scope="scope">
                <span v-if="scope.row.type == 1">早餐</span>
                <span v-if="scope.row.type == 2">午餐</span>
                <span v-if="scope.row.type == 3">晚餐</span>
                <span v-if="scope.row.type == 4">接待餐</span>
<!--                <span v-if="scope.row.type == 5">其它</span>-->
            </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:diningtype:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:diningtype:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改就餐类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属公司" prop="companyId">
          <el-select v-model="form.companyId" placeholder="请选择所属公司" clearable size="small"
            @change="getComboBoxWithStatusByCompanyId"
            style="width: 100%;" >
<!--            style="width: 100%;" :disabled="notModifyCompany">-->
            <el-option v-for="item in companys" :key="item.id" :label="item.text" :value="Number(item.id)"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="就餐类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择就餐类型" clearable size="small" style="width: 100%;" :disabled="notModifyCompany">
          <el-option label="早餐" :value="1" />
          <el-option label="午餐" :value="2" />
          <el-option label="晚餐" :value="3" />
          <el-option label="接待餐" :value="4" />
          <el-option label="其它" :value="5" />
        </el-select>
        </el-form-item> -->

        <el-form-item label="就餐类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择就餐类型" clearable size="small" style="width: 100%;" >
<!--            :disabled="notModifyCompany"-->
            <el-option v-for="item in comboBoxWithStatus" :key="item.id"
                :label="item.text" :value="Number(item.id)"></el-option>
<!--         :disabled="item.disabled"   -->
        </el-select>
        </el-form-item>

        <el-form-item label="开始时间" >
          <el-time-select style="width: 100%;"
            placeholder="开始时间"
            v-model="form.startTime"
            :picker-options="{
              start: '06:30',
              step: '00:15',
              end: '23:30'
            }">
        </el-time-select>
        </el-form-item>

        <el-form-item label="结束时间" >
          <el-time-select style="width: 100%;"
            placeholder="结束时间"
            v-model="form.endTime"
            :picker-options="{
              start: '06:30',
              step: '00:15',
              end: '23:30',
              minTime: form.startTime
            }">
        </el-time-select>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType, exportType, getComboBoxWithStatus } from "@/api/diningType/type";
import { getCompanys } from '@/api/system/dept';

export default {
  name: "Type",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        startTime: undefined,
        endTime: undefined,
        date: '2021-06-01'
      },
      // 表单参数
      form: {},
       // 公司信息
      companys: [],
      // 公司能否修改
      notModifyCompany: false,
      // 带禁用状态的就餐类型下拉框
      comboBoxWithStatus: [],
      // 表单校验
      rules: {
        type: [
            { required: true, message: '请选择就餐类型', trigger: 'change' }
        ],
        companyId: [
            { required: true, message: '请选择所属公司', trigger: 'change' }
        ],
        // startTime: [
        //     { required: true, message: '请选择时间', trigger: 'change' }
        // ],
      }
    };
  },
  created() {
    this.getList();
    this.getCompanyList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.typeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        startTime: undefined,
        endTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.notModifyCompany = false;
      this.title = "添加就餐类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getType(id).then(response => {
        let {companyId} = response.data;
        this.form = response.data;
        this.getComboBoxWithStatusByCompanyId(companyId);
        this.notModifyCompany = true;
        this.open = true;
        this.title = "修改就餐类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateType(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addType(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该就餐类型', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delType(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportType(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    },
    /** 获取公司信息 */
    getCompanyList() {
      getCompanys().then(response => {
        this.companys = response.data;
      });
    },
    /** 获取带禁用状态的就餐类型下拉框 */
    getComboBoxWithStatusByCompanyId(companyId) {
      getComboBoxWithStatus(companyId).then(response => {
        this.comboBoxWithStatus = response.data;
      });
    },
  }
};
</script>
